var crypto = require('crypto')

exports.encipher = function (data, secret, encoding, algorithm) {
  var cipher = crypto.createCipher(algorithm || 'AES128', secret)
  cipher.update(data, 'utf8')
  return cipher.final(encoding || 'hex')
}

exports.decipher = function (ciphertext, secret, inputcoding, encoding, algorithm) {
  var cipher = crypto.createDecipher(algorithm || 'AES128', secret)
  cipher.update(ciphertext, inputcoding || 'hex')
  return cipher.final(encoding || 'utf8')
}

exports.hmac = function ( data, secret, encoding, algorithm) {
  var h = crypto.createHmac(algorithm || 'SHA1', secret)
  h.update(data)
  return h.digest(encoding || 'hex')
}

exports.hash = function (data, encoding, algorithm) {
  var h = crypto.createHash(algorithm || 'SHA1')
  h.update(data)
  return h.digest(encoding || 'hex')
}

exports.randomBytes = function  (len, encoding) {
  return crypto.randomBytes(len || 32).toString(encoding || 'hex')
}
